package libaryinterface.user;

import libaryinterface.admin.AdminAddUserInterface;
import libaryinterface.admin.AdminInterface;
import libaryinterface.interfaceutils.MyDialogDemo;
import pojo.User;
import pojo.vo.UserVo;
import service.UserService;
import service.impl.UserServiceImpl;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

//用户信息界面
public class UseinfoInterface extends JFrame {
    int i=0;
    UserService userService = new UserServiceImpl();

    public UseinfoInterface() throws SQLException
    {
        super("用户信息窗口");

        //1.把图片添加到标签里（把标签的大小设为和图片大小相同），把标签放在分层面板的最底层；
        ImageIcon bg = new ImageIcon("src//picture//excel.jpg");
        JLabel label = new JLabel(bg);
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        UseinfoInterface.super.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //2.把窗口面板设为内容面板并设为透明、流动布局。
        JPanel p1=(JPanel)UseinfoInterface.super.getContentPane();
        p1.setOpaque(false);
        p1.setLayout((new FlowLayout()));

        Container container = this.getContentPane();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(500,500,1000,800);


        setBounds(1000,1000,1500,1200);
        Object[][] rowData = new Object[100][6];
        List<UserVo> userList = userService.listUser();

        String[] columnName = {"账号","密码","是否是管理","昵称","身份","已借书籍数"};
        for(UserVo user : userList)
        {
            String account = user.getAccount();
            String password = user.getPassword();
            Long isCaretaker = user.getIsCaretaker();
            String nickName = user.getNickName();
            String categoryId = user.getcName();
            Long aleadyNumber = user.getAlreadyNumber();

            rowData[i][0]= account;
            rowData[i][1]= password;
            rowData[i][2]= isCaretaker;
            rowData[i][3]= nickName;
            rowData[i][4]= categoryId;
            rowData[i][5]= aleadyNumber;
            i++;
        }

        JTable table = new JTable(rowData,columnName);

        table.setForeground(Color.BLACK);                   // 字体颜色
        table.setFont(new Font(null, Font.PLAIN, 14));      // 字体样式
        table.setSelectionForeground(Color.DARK_GRAY);      // 选中后字体颜色
        table.setSelectionBackground(Color.YELLOW);     // 选中后字体背景
        table.setGridColor(Color.GRAY);                     // 网格颜色

        table.getTableHeader().setFont(new Font(null, Font.BOLD, 20));  // 设置表头名称字体样式
        table.getTableHeader().setForeground(Color.RED);                // 设置表头名称字体颜色
        table.getTableHeader().setResizingAllowed(true);               // 设置不允许手动改变列宽
        table.getTableHeader().setReorderingAllowed(false);             // 设置不允许拖动重新排序各列

        // 设置行高
        table.setRowHeight(40);

        // 第一列列宽设置为40
        table.getColumnModel().getColumn(0).setPreferredWidth(40);

        // 设置滚动面板视口大小（超过该大小的行数据，需要拖动滚动条才能看到）
        table.setPreferredScrollableViewportSize(new Dimension(400, 300));

        // 把 表格 放到 滚动面板 中（表头将自动添加到滚动面板顶部）
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBounds(240, 180, 1000, 600);
        // 添加 滚动面板 到 内容面板
        container.add(scrollPane);
        Font font = new Font("黑体", Font.BOLD, 20);

        JButton jButton1 = new JButton("添加");
        jButton1.setBounds(600,820,80,60);
        jButton1.setFont(font);
        container.add(jButton1);
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    //进入管理员添加用户的界面
                    new AdminAddUserInterface();
                }catch (Exception ex)
                {
                    System.out.println("添加失败");
                    ex.printStackTrace();
                }
            }
        });

        //删除按钮
        JButton jButton2 = new JButton("删除");
        jButton2.setBounds(700,820,80,60);
        jButton2.setFont(font);
        container.add(jButton2);
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index = 0;
                try{
                    index = table.getSelectedRow();
                    if(index>=i){
                        new MyDialogDemo("所选行无信息！");
                    }
                    else{
                        String Useraccount = (String) rowData[index][0];
                        //进入确认删除的界面
                        new UserIsDeleteInterface(Useraccount);
                    }
                }catch (Exception ex1)
                {
                    ex1.printStackTrace();
                    new MyDialogDemo("未选中行！");
                }
            }
        });

        JButton button3 = new JButton("修改");
        button3.setFont(font);
        button3.setBounds(800,820,80,60);
        container.add(button3);
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int index = 0;
                try{
                    index = table.getSelectedRow();
                    if(index >= i)
                    {
                        new MyDialogDemo("请选中有信息的行！");
                    }else {
                        new UserIsUpdateInterface(rowData,index);
                    }
                }catch (Exception ex2){
                    ex2.printStackTrace();
                    new MyDialogDemo("未选中行！");
                }
            }
        });

        //刷新按钮
        JButton button4 = new JButton("刷新");
        button4.setBounds(50,30,80,50);
        button4.setFont(font);
        container.add(button4);
        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UseinfoInterface.super.dispose();
                try {
                    new UseinfoInterface();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        JButton jButton5 = new JButton("查询");
        jButton5.setFont(font);
        jButton5.setBounds(1300,100,80,40);
        container.add(jButton5);

        JLabel jLabel = new JLabel("输入您要查找的账号：");
        Font font3 = new Font("隶书",Font.PLAIN,24);
        jLabel.setFont(font3);
        jLabel.setBounds(800,100,500,40);
        jLabel.setForeground(Color.GREEN);
        container.add(jLabel);

        JButton jButton6 = new JButton("返回上个界面");
        jButton6.setFont(font3);
        jButton6.setBounds(100,820,250,60);
        container.add(jButton6);
        jButton6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AdminInterface();
                UseinfoInterface.super.dispose();
            }
        });

        //用户查找界面
        JTextField find = new JTextField(100);
        find.setBounds(1100,100,130,40);
        find.setFont(font);
        container.add(find);
        jButton5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try
                {
                    new FinduesInterface(find.getText());
                }catch (SQLException ex)
                {
                    throw new RuntimeException(ex);
                }
            }
        });


        //获得容器
        container.setBackground(new Color(50, 144, 236));
        setBounds(180, 1, 1500, 1200);
        container.setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
}
